|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
OLE Automation With An Embedded Object To demonstrate OLE automation with an embedded object, I will use a Microsoft Word document object. The technique of combining OLE embedding with OLE automation is both powerful and flexible. This simple demonstration gives you a taste of what is possible. The program does the following:
Figure 10.9 shows this demonstration program, displaying the document activated for editing. Notice that the activated document displays Words menu and provides access to Words formatting and editing commands. The programs objects and properties are presented in Listing 10.4, and the code is in Listing 10.5. Rather than walk you though the code a line at a time, I suggest you try to figure out whats going on by yourself. The code is actually fairly simple, so you shouldnt have any problems.
Listing 10.4 Objects and properties in OLEAUTO2.FRM.
Begin VB.Form Form1
Caption = Form1
LinkTopic = Form1
Begin VB.CommandButton Command1
Caption = &Done
End
Begin VB.CommandButton Command1
Caption = &Finish Letter
Index = 1
End
Begin VB.CommandButton Command1
Caption = &Begin Letter
Index = 0
End
Begin VB.OLE OLE1
End
End
Listing 10.5 Code in OLEAUTO2.FRM.
Option Explicit
The Object variable.
Dim WordDoc As Object
Const vbOLEPrimary = 0
Private Sub Command1_Click(Index As Integer)
Dim Reply As Integer
Dim s1 As String, s2 As String
Five tabs.
s1 = Chr$(9) & Chr$(9) & Chr$(9) & Chr$(9) & Chr$(9)
Select Case Index
Case 0 Begin Letter.
Insert the date into the document.
WordDoc.Insert Date$
WordDoc.InsertPara
Insert two blank lines.
WordDoc.InsertPara
WordDoc.InsertPara
Insert the return address preceded by tabs.
s2 = s1 & P.O. Box 1234
WordDoc.Insert s2
WordDoc.InsertPara
s2 = s1 & Anytown, NC 12345
WordDoc.Insert s2
WordDoc.InsertPara
Insert two blank lines and the greeting.
WordDoc.InsertPara
WordDoc.InsertPara
WordDoc.Insert Dear
Activate for in-place editing.
OLE1.DoVerb (vbOLEPrimary)
Case 1 Finish Letter.
Add the salutation at the end of the letter.
WordDoc.InsertPara
s2 = s1 & Yours truly,
WordDoc.Insert s2
WordDoc.InsertPara
WordDoc.InsertPara
WordDoc.InsertPara
WordDoc.InsertPara
s2 = s1 & Peter G. Aitken
WordDoc.Insert s2
WordDoc.InsertPara
Activate for in-place editing.
OLE1.DoVerb (vbOLEPrimary)
Case 2 Done
Does the user want to print? If so, instruct
Word to print with the default settings.
Reply = MsgBox(Print letter?, vbYesNo + vbQuestion)
If Reply = vbYes Then WordDoc.FilePrintDefault
Does the user want to save? If so, instruct
Word to display the Save As dialog box.
Reply = MsgBox(Save letter?, vbYesNo + vbQuestion)
If Reply = vbYes Then
WordDoc.FileSave
End If
Destroy the object.
Set WordDoc = Nothing
End
End Select
End Sub
Private Sub Form_Load()
Position the OLE control to fill the width of the form
and 3/4 of the height.
OLE1.Top = 0
OLE1.Left = 0
OLE1.Width = ScaleWidth
OLE1.Height = ScaleHeight * 0.75
Create a new embedded Word document.
OLE1.CreateEmbed , Word.Document.6
Activate the object(required for OLE automation).
OLE1.DoVerb (vbOLEPrimary)
Create the OLE automation object.
Set WordDoc = OLE1.object.Application.WordBasic
End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |